using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace MiddleScene
{
    public class PanelBattle : IPanel
    {
        private Slider SliderHp;
        private Slider SliderMp;
        private Slider SliderArmor;
        private TextMeshProUGUI TextHp;
        private TextMeshProUGUI TextMp;
        private TextMeshProUGUI TextArmor;
        private TextMeshProUGUI TextGem;
        public PanelBattle(IPanel parent) : base(parent)
        {
            m_GameObject = UnityTool.Instance.GetGameObjectFromCanvas(GetType().Name);
            children.Add(new PanelPause(this));
            children.Add(new PanelGemStore1(this));
            children.Add(new PanelSafeBox(this));
            children.Add(new PanelLuckyCat(this));
            children.Add(new PanelBackpack(this));

            EventCenter.Instance.RegisterObserver(EventType.OnWantUseSafeBox, () =>
            {
                EnterPanel(typeof(PanelSafeBox));
            });
          
            EventCenter.Instance.RegisterObserver(EventType.OnWantUseFridge, () =>
            {
                EnterPanel(typeof(PanelGemStore1));
            });
            EventCenter.Instance.RegisterObserver(EventType.OnUseLuckyCat, () =>
            {
                EnterPanel(typeof(PanelLuckyCat));
            });
        }
        protected override void OnInit()
        {
            base.OnInit();
            SliderHp = UnityTool.Instance.GetComponentFromChild<Slider>(m_GameObject, "SliderHp");
            SliderMp = UnityTool.Instance.GetComponentFromChild<Slider>(m_GameObject, "SliderMp");
            SliderArmor = UnityTool.Instance.GetComponentFromChild<Slider>(m_GameObject, "SliderArmor");
            TextHp = UnityTool.Instance.GetComponentFromChild<TextMeshProUGUI>(SliderHp.gameObject, "Text");
            TextMp = UnityTool.Instance.GetComponentFromChild<TextMeshProUGUI>(SliderMp.gameObject, "Text");
            TextArmor = UnityTool.Instance.GetComponentFromChild<TextMeshProUGUI>(SliderArmor.gameObject, "Text");
            TextGem = UnityTool.Instance.GetComponentFromChild<TextMeshProUGUI>(m_GameObject, "TextGem");
            UnityTool.Instance.GetComponentFromChild<Button>(m_GameObject, "ButtonPause").onClick.AddListener(() =>
            {
                EnterPanel(typeof(PanelPause));
                m_GameObject.SetActive(false);
            });
        }
        protected override void OnUpdate()
        {
            base.OnUpdate();
            var player = GetPlayer();
            if (player != null && player.m_Attr != null)
            {
                SliderHp.value = player.m_Attr.CurrentHp / (float)player.m_Attr.m_ShareAttr.MaxHp;
                SliderArmor.value = player.m_Attr.CurrentArmor / (float)player.m_Attr.m_ShareAttr.Armor;
                SliderMp.value = player.m_Attr.CurrentMp / (float)player.m_Attr.m_ShareAttr.Magic;
                TextHp.text = player.m_Attr.CurrentHp + "/" + player.m_Attr.m_ShareAttr.MaxHp;
                TextMp.text = player.m_Attr.CurrentMp + "/" + player.m_Attr.m_ShareAttr.Magic;
                TextArmor.text = player.m_Attr.CurrentArmor + "/" + player.m_Attr.m_ShareAttr.Armor;
            }
            else
            {
                // 如果玩家為空，顯示默認值
                SliderHp.value = 0;
                SliderArmor.value = 0;
                SliderMp.value = 0;
                TextHp.text = "0/0";
                TextMp.text = "0/0";
                TextArmor.text = "0/0";
                
                // 調試信息
                if (player == null)
                {
                    Debug.LogWarning("PanelBattle: Player is null");
                }
                else if (player.m_Attr == null)
                {
                    Debug.LogWarning("PanelBattle: Player.m_Attr is null");
                }
            }
            TextGem.text = ArchiveCommand.Instance.GetMaterialNum(MaterialType.Gem).ToString();
            if(Input.GetKeyDown(KeyCode.B))
            {
                EnterPanel(typeof(PanelBackpack));
            }
        }
        private IPlayer GetPlayer()
        {
            var playerController = GameMediator.Instance.GetController<PlayerController>();
            if (playerController != null)
            {
                var player = playerController.Player;
                if (player != null)
                {
                    Debug.Log($"PanelBattle.GetPlayer - Player found: {player.gameObject.name}, Hp: {player.m_Attr?.CurrentHp}");
                }
                else
                {
                    Debug.LogWarning("PanelBattle.GetPlayer - PlayerController exists but Player is null");
                }
                return player;
            }
            else
            {
                Debug.LogWarning("PanelBattle.GetPlayer - PlayerController is null");
            }
            return null;
        }
    }

}
